package cn.ante.org.service.impl;

import cn.ante.basic.service.impl.BaseServiceImpl;
import cn.ante.basic.utils.MD5Utils;
import cn.ante.basic.utils.StrUtils;
import cn.ante.org.domain.Employee;
import cn.ante.org.mapper.EmployeeMapper;
import cn.ante.org.service.IEmployeeService;
import cn.ante.user.domain.Logininfo;
import cn.ante.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    @Transactional
    public void add(Employee employee) {
        // 将他初始化
        initEmployee(employee);
        employeeMapper.add(employee);
        // 将其拷贝
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.add(logininfo);
        // 将logininfo的id,设置到employee中
        employee.setLogininfoId(logininfo.getId());
        // 最后
        employeeMapper.update(employee);
        initRelationTable(employee);
    }

    public void initRelationTable(Employee employee){
        // 可能存在用户没有选择角色或者说没有修改角色的情况
        if (employee.getRoles() != null) {
            // 在不知道用户是否添加角色情况下，先将该用户下角色表中的信息删除，再做新增处理
            // 删除角色对应的角色表数据
            employeeMapper.removeRole(employee.getId());
            // 删除成功后再将员工对象中封装的集合存到对应的表数据中
            employeeMapper.saveRole(employee.getId(), employee.getRoles());
        }
    }

    private void initEmployee(Employee employee){
        // 获取颜值
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);
        // 根据颜值进行加密
        String pwd = MD5Utils.encrypByMd5(salt + employee.getPassword());
        employee.setPassword(pwd);
    }

    private Logininfo employee2Logininfo(Employee employee){
        Logininfo logininfo = new Logininfo();
        // 使用工具类拷贝
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        return logininfo;
    }


//
    @Override
    @Transactional
    public void update(Employee employee) {
        Logininfo logininfo = logininfoMapper.loadById(employee.getLogininfoId());
        if (logininfo != null){
            BeanUtils.copyProperties(employee,logininfo);
            logininfoMapper.update(logininfo);
            employeeMapper.update(employee);
        }
        initRelationTable(employee);

    }
//
    @Override
    @Transactional
    public void remove(Long id) {
        Employee employee = employeeMapper.loadById(id);
        if (employee != null) {
            logininfoMapper.remove(employee.getLogininfoId());
            employeeMapper.remove(id);
        }
    }


    @Override
    public Employee loadByLoginifoId(Long id) {
        return employeeMapper.loadByLoginifoId(id);
    }
}
